package field;

import javax.swing.*;
import java.awt.*;

import piece.*;

public class Board extends JPanel implements Cloneable {
    // the path of the board image file
    private static final String boardPath = "src/image/board.JPG";
    private static final Image board = new ImageIcon(boardPath).getImage();

    public static final int width = 600;
    public static final int height = 600;
    public static final int distance = 39;
    private static final int startX = 26;
    private static final int startY = 26;

    private Piece[][] pieces = new Piece[15][15];

    public static int getCount() {
        return count;
    }

    private static int count = 0; //The number of pieces on the board

    public Board() {
        clearAll();
    }

    public void clearAll() {
        for (int i = 0; i < 15; ++i) {
            for (int j = 0; j < 15; ++j) {
                pieces[i][j] = null;
            }
        }
        count = 0;
    }

    public void place(int x, int y, Piece piece) {
        pieces[x][y] = piece;
        count++;
    }

    public boolean canPlace(int x, int y) {
        boolean ret = true;
        if (pieces[x][y] != null) {
            ret = false;
        }
        return ret;
    }

    /**
     * count the mount of the pieces with the same color and the same x
     *
     * @return the count
     */
    public int checkX(int x, int y, boolean color) {
        int countX = 1;

        if (y - 1 > 0) {
            for (int j = y - 1; j >= 0; --j) {
                Piece piece = pieces[x][j];
                if (piece != null && piece.getColor() == color) {
                    countX++;
                } else {
                    break;
                }
            }
        }
        if (y + 1 < 15 && countX < 5) {
            for (int j = y + 1; j < 15; ++j) {
                Piece piece = pieces[x][j];
                if (piece != null && piece.getColor() == color) {
                    countX++;
                } else {
                    break;
                }
            }
        }

        return countX;
    }

    /**
     * count the mount of the pieces with the same color and the same y
     *
     * @return the count
     */
    public int checkY(int x, int y, boolean color) {
        int countY = 1;

        if (x - 1 > 0) {
            for (int i = x - 1; i >= 0; --i) {
                Piece piece = pieces[i][y];
                if (piece != null && piece.getColor() == color) {
                    countY++;
                } else {
                    break;
                }
            }
        }
        if (x + 1 < 15 && countY < 5) {
            for (int i = x + 1; i < 15; ++i) {
                Piece piece = pieces[i][y];
                if (piece != null && piece.getColor() == color) {
                    countY++;
                } else {
                    break;
                }
            }
        }

        return countY;
    }

    /**
     * count the mount of the pieces with the same color
     * and the same left oblique.
     *
     * @return the count
     */
    public int checkZL(int x, int y, boolean color) {
        int countZL = 1;
        for (int i = 1; i < 15; ++i) {
            if (x - i > 0 && y - i > 0) {
                Piece piece = pieces[x - i][y - i];
                if (piece != null && piece.getColor() == color) {
                    countZL++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }

        if (countZL < 5) {
            for (int i = 1; i < 15; ++i) {
                if (x + i < 15 && y + i < 15) {
                    Piece piece = pieces[x + i][y + i];
                    if (piece != null && piece.getColor() == color) {
                        countZL++;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
        return countZL;
    }

    /**
     * count the mount of the pieces with the same color
     * and the same right oblique.
     *
     * @return the count
     */
    public int checkZR(int x, int y, boolean color) {
        int countZR = 1;
        for (int i = 1; i < 15; ++i) {
            if (x + i < 15 && y - i > 0) {
                Piece piece = pieces[x + i][y - i];
                if (piece != null && piece.getColor() == color) {
                    countZR++;
                } else {
                    break;
                }
            } else {
                break;
            }
        }
        if (countZR < 5) {
            for (int i = 1; i < 15; ++i) {
                if (x - i > 0 && y + i < 15) {
                    Piece piece = pieces[x - i][y + i];
                    if (piece != null && piece.getColor() == color) {
                        countZR++;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
        return countZR;
    }

    /**
     * clear the piece at the specified position
     */
    public void clear(int x, int y) {
        pieces[x][y] = null;
        count--;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    /**
     * draw the board according to the image.
     */
    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(board, 0, 0, null);
        for (int i = 0; i < 15; ++i) {
            for (int j = 0; j < 15; ++j) {
                if (pieces[i][j] != null) {
                    pieces[i][j].draw(g, i * distance + startX, j * distance + startY);
                }
            }
        }
    }
}
